{extend name="platform/base" /}
{block name="resources"/}
<link rel="stylesheet" href="PLATFORM_CSS/areaManagement.css">
<style>
.modal-body table tr td {line-height: 25px; padding: 5px;}
.modal-body table tr td:first-child {width: 30%; text-align: right;}
.modal-body table tr td:last-child {width: 70%;}
</style>
{/block}
 {block name="main"}
<div class="row padder-v">
	<div class="col-sm-3"></div>
	<div class="col-sm-2 js-shop-block" >
			<select id="point_exchange_id" class="input-sm form-control" >
				<option value="">请选择积分状态</option>
				<option value="1">只能积分兑换</option>
				<option value="0">非积分兑换</option>
			</select>
	</div>


	<div class="col-sm-2 js-shop-block" >
			<select id="shop_id" class="input-sm form-control" >
				<option value="">请选择店铺名称</option>
				{foreach name="shoplist" item ="vo"}
				<option value="{$vo.shop_id}">{$vo.shop_name}</option>
				{/foreach}
			</select>
	</div>
	
	<div class="col-sm-2">
		<input type="text" id="search_text" class="input-sm form-control" placeholder="请输入商品名称"> 
	</div>
	
	<div class="col-sm-2 js-shop-block" >
		<select class="input-sm form-control" id="shop_type" >
			<option value="">请选择商品状态</option>
            <option value="">全部</option>
            <option value="-1">待审核</option>
			<option value="1">上架</option>
			<option value="0">下架</option>
       </select>	
	</div>
	
	<div class="col-sm-1">
		<button class="btn btn-sm btn-default" type="button" onclick="LoadingInfo(1)">搜索</button>
	</div>
</div>
<section class="panel panel-default">
	<div class="table-responsive">
		<table class="table table-striped b-light text-sm">
			<colgroup>
				<col width="3%">
				<col width="5%">
				<col width="8%">
				<col width="8%">
				<col width="10%">
				<col width="8%">
				<col width="8%">
				<col width="8%">
				<col width="7%">
				<col width="13%">
			</colgroup>
			<thead>
				<tr>
					<th><input type="checkbox"></th>
					<th>图片</th>
					<th class="left">商品名称</th>

					<th>分类&品牌</th>
					<th>积分</th>
					<th class="right">价格</th>
					<th class="right">库存</th>
					<th class="center">商品状态</th>
					<th class="center">兑换状态</th>
					<th class="center">操作</th>
				</tr>
			</thead>
			<tbody id="list">
				
			</tbody>
		</table>
	</div>


<!-- 积分兑换开始 -->
	<div class="modal fade" id="setPointChange">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					<h4id="myModalLabel">积分兑换设置</h4>
				</div>
				<div class="modal-body">
					<table>
						<tbody>
							<tr>
								<td class="right">
									<span style="color:red;"></span>
									兑换方式：
								</td>
								<td>
									<select style="width:150px;" id="point_exchange_type">
										<!--<option value="" selected>请选择兑换类型</option> -->
										<option value="0">非积分兑换</option>
										<option value="1">只能积分兑换</option>
									</select>
								</td>
							</tr>
							<tr>
								<td class="right">积分兑换：</td>
								<td><input type="text" id="point_exchange" maxlength="6" onkeyup="this.value=this.value.replace(/\D/g,'')"></td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="modal-footer">
					<input type="hidden" id="goods_id">
					<button type="button" class="btn btn-success" onclick="setPointChange(0,0,0,1)">保存</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>


	{include file="platform/page" /}
</section>
{/block} 
{block name="script"}
<link rel="stylesheet" type="text/css" href="__ROOT__/public/admin/css/plugin/jquery.searchableSelect.css">
<script src="__ROOT__/public/admin/js/plugin/jquery.searchableSelect.js"></script>
<script src="__STATIC__/js/BootstrapMenu.min.js"></script>

<script type="text/javascript">
//当前可搜索的下拉选项框
var curr_searchable_select = null;
$(function(){
	LoadingInfo(1);
});

	//查询用户列表
	function LoadingInfo(pageIndex) {
		
		var shop_id = $("#shop_id").val();
		var goods_name =$("#search_text").val();
		var state = $("#shop_type").val();
		var point_exchange_type = $("#point_exchange_id").val();
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/promotion/pointchange')}",
			async : true,
			data : {
				"pageIndex" : pageIndex, 
				"shop_id" : shop_id, 
				"goods_name" : goods_name, 
				"state" : state,
				"point_exchange_type" : point_exchange_type
			},
			success : function(data) {
				var html = '';
				$("#total_count_num").text(data["total_count"]);
				$("#page_count_num").text(data["page_count"]);
				$("#page_count").val(data["page_count"]);
				$("#pageNumber a").remove();
				
				if (data["data"].length > 0) {
					for (var i = 0; i < data["data"].length; i++) {
						html += '<tr>';
						html += '<td><input type="checkbox" name="sub" value="'+ data['data'][i]['goods_id']+'"></td>';
						html += '<td><img src="'+__IMG(data["data"][i]["pic_cover_micro"])+'"></td>';
						html += '<td>'+data["data"][i]["goods_name"]+'</td>';
						
						if(data["data"][i]["brand_name"] != null){
							html += '<td><div class="media-body"><div>'+data["data"][i]["category_name"]+'</div><small class="text-muted">'+data["data"][i]["brand_name"]+'</small></div></td>';
						}else
						if(data["data"][i]["category_name"] != null){
							html += '<td><div class="media-body"><div>'+data["data"][i]["category_name"]+'</div></div></td>';
						}else{
							html += '<td class="left"><div class="media-body"><div>--</div></div></td>';
						}
						html += '<td>'+data["data"][i]["point_exchange"]+'</td>';
						html += '<td class="right">' + data["data"][i]["price"] + '</td>';
						html += '<td class="right">' + data["data"][i]["stock"] + '</td>';
						if(data["data"][i]["state"] == 1){
							html += '<td class="center">已上架</td>';
						}else if(data["data"][i]["state"] == 10 || data["data"][i]["state"] == 0){
							html += '<td class="center">已下架</td>';
						}else if(data["data"][i]["state"] == -1){
                            html += '<td class="center">待审核</td>';
                        }
						
						if(data["data"][i]["point_exchange_type"] == 1){
							html += '<td class="center">积分兑换</td>';
						}else{
							html += '<td class="center">非积分兑换</td>';
						}
						html += '<td class="center">';
						if(data["data"][i]["point_exchange_type"] == 1){
							html += ' <a href="javascript:void(0);" onclick="setPointChange('+data["data"][i]["goods_id"]+', '+data["data"][i]["point_exchange_type"]+', '+data["data"][i]["point_exchange"]+', 0)">积分兑换设置</a>';
						}else{
							html += ' <a href="javascript:void(0);" onclick="setPointChange('+data["data"][i]["goods_id"]+', '+data["data"][i]["point_exchange_type"]+', '+data["data"][i]["point_exchange"]+', 0)">启动积分兑换</a>';
						}
							
						html += '</td>';
						html += '</tr>';
					}
				} else {
					html += '<tr align="center"><td colspan="13">暂无商品数据记录</td></tr>';
				}
				$("#list").html(html);
				var totalpage = $("#page_count").val();
				if (totalpage == 1) {
					changeClass("all");
				}
				var $html = pagenumShow(jumpNumber,totalpage,{$pageshow})
				$("#pageNumber").append($html);
			}
		});
	}
	
	
	//设置积分兑换
	function setPointChange(goods_id,point_exchange_type,point_exchange,status){
		$("#setPointChange").modal('show');
		if(goods_id!=0){
			$("#goods_id").val(goods_id);
			var data = $("#point_exchange_type option");
			for(var i=0;i<data.length;i++){
				var value = data.eq(i).val();
				if(value == point_exchange_type){
					data.eq(i).prop('selected',true);
				}
			}

			
			$("#point_exchange").val(point_exchange);
		}
		var goods_id = $("#goods_id").val();
		var point_exchange_type = $("#point_exchange_type").val();
		var point_exchange = $("#point_exchange").val();

		if(status == 1){
			$.ajax({
				type : "post",
				url : "{:__URL('PLATFORM_MAIN/promotion/setPointChange')}",
				async : true,
				data : {
					"goods_id" : goods_id, "point_exchange_type" : point_exchange_type,  "point_exchange" : point_exchange
				},
				success : function(data) {
					if(data["code"] > 0 ){
						showMessage('success', "设置成功","{:__URL('PLATFORM_MAIN/promotion/pointchange')}");
					}else{
						showMessage('error', "设置失败");
					}
				}
			})
		}
	}


	$("#search_text").keypress(function(e){
		if(e.keyCode == 13){
			LoadingInfo(1);
		}
	});
</script>
{/block}
